package com.star.book.utils;

import com.star.book.entity.StarBook;
import com.star.book.entity.User;
import com.star.book.service.IBookInfoService;
import com.star.book.service.IStarBookService;
import com.star.book.service.IUserService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * 网站日志
 * */
public class TimedTasks implements Job {


    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            IUserService userService = (IUserService)SpringUtil.getBean(IUserService.class);
           int userCount = userService.selectUserCount();
            IBookInfoService bookInfoService = (IBookInfoService)SpringUtil.getBean(IBookInfoService.class);
            int bookCount = bookInfoService.selectBookInfoCount();//小说数量
            int onlineUserCount = userService.selectUserStatusCount();//查看用户在线人数
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            //设置星小说日志信息
            StarBook starBook = new StarBook();
            starBook.setBookCount(bookCount);
            starBook.setStarBookTime(df.format(new Date()));
            starBook.setUserCount(userCount);
            starBook.setUserOnline(onlineUserCount);
            IStarBookService starBookService = (IStarBookService)SpringUtil.getBean(IStarBookService.class);
            List<StarBook> starBooks = starBookService.selectStarBookByBookCount();
            Integer bookCount1 = starBooks.get(0).getBookCount();
            Integer bookCount2 = starBooks.get(1).getBookCount();
            int bookCountAdd = bookCount1 - bookCount2;
            starBook.setBookCountAdd(bookCountAdd);
            starBookService.save(starBook);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("数据设置完毕！");
    }
}
